import tools from "@/common/tools.js";
import {
	post
} from '@/common/axios.js';
import {
	getdiamonds, //钻石列表
	paybuyDiamond, //购买钻石
	// 提现账户
	userAccount,
	// 添加账户
	addAccount,
	// 修改账户
	updateAccount,
	//删除账户
	removeAccount,
	// 申请提现
	applyWithdrawal,
	// 提现记录
	withdrawalRecord,
	//会员列表
	vipList,
	//购买会员
	addVipOrder,
	//收入指出流水- 钻石清单 - 收益
	getaccountLog
} from '@/common/path.js'
export default {
	namespaced: true,
	state: {
		diamondsList: [], //钻石列表
		userInfo: uni.getStorageSync('userInfo'), //用户信息
		accountData: {}, //账户
		withdrawList: [], //提现记录列表
		vips: [], //vip套餐列表
		payMessageList:[],//钻石清单
		monthIncome:'',//收益统计
	},
	getters: {

	},
	mutations: {
		SET_DIAMONDS_LIST(state, data) { //钻石列表
			state.diamondsList = data
		},
		SET_ACCOUNT(state, data) { //账户
			state.accountData = data
		},
		SET_WITHDRAW(state, data) { //账户
			state.withdrawList = data
		},
		SET_VIP_LIST(state, data) { //vip套餐列表
			state.vips = data
		},
		SET_MESSAGE_LIST(state, data) { //钻石清单-  支出 - 收入 明细
			state.payMessageList = data
		},
		SET_MONTHINCOME(state, data) { //钻石清单-  支出 - 收入 明细
			state.monthIncome = data
		},
		

	},
	actions: {
		//钻石清单
		getaccountLog({
			dispatch,
			commit,
			state
		}, args = {}) {
			post(getaccountLog, args).then(res => {
				if (res.code == 0) {
						commit('SET_MONTHINCOME', res.data.monthIncome);
					if (args.pageNum == 1) {
						commit('SET_MESSAGE_LIST', res.data.records);
					} else {
						state.payMessageList.push(...res.data.records)
						commit('SET_MESSAGE_LIST', state.payMessageList);
					}
				} else {
					commit('SET_MESSAGE_LIST', [])
				}
			})
		},

		//钻石列表
		getdiamondsApi({
			dispatch,
			commit,
		}, args) {
			post(getdiamonds, args).then(res => {
				if (res.code == 0) {
					commit('SET_DIAMONDS_LIST', res.data)
				} else {
					commit('SET_DIAMONDS_LIST', [])
				}
			})
		},
		//购买钻石
		paybuyDiamond({
			dispatch
		}, args) {
			post(paybuyDiamond, args).then(res => {
				if (res.code == 0) {
					let data = {
						data: res.data,
						type: args.payType
					}
					dispatch('payData', data)
				} else {
					tools.showToast(res.msg)
				}
			})
		},
		// 支付
		payData({
			dispatch
		}, res) {
			let that = this
			uni.requestPayment({
				provider: res.type,
				orderInfo: res.data,
				success: (res) => {
					tools.showToast('成功')
					setTimeout(r => {
						tools.back()
					}, 1000)
				},
				fail: (res) => {
					console.log(res, 'fail');
				}
			})
		},
		//添加账户
		addAccount({
			dispatch
		}, args) {
			post(addAccount, args).then(res => {
				if (res.code == 0) {
					tools.showToast(res.msg)
					setTimeout(() => {
						tools.back();
					}, 1000)
				} else {
					tools.showToast(res.msg)
				}
			})
		},
		//修改账户
		updateAccount({
			dispatch
		}, args) {
			post(updateAccount, args).then(res => {
				if (res.code == 0) {
					tools.showToast(res.msg)
					setTimeout(() => {
						tools.back();
					}, 1000)
				} else {
					tools.showToast(res.msg)
				}
			})
		},
		//删账户
		removeAccount({
			dispatch
		}, args) {
			post(removeAccount, args).then(res => {
				if (res.code == 0) {
					tools.showToast(res.msg)
					setTimeout(() => {
						tools.back();
					}, 1000)
				} else {
					tools.showToast(res.msg)
				}
			})
		},
		//查询账户
		getAccount({
			dispatch,
			commit
		}, args = {}) {
			post(userAccount, args).then(res => {
				if (res.code == 0) {
					commit('SET_ACCOUNT', res.data)
				} else {
					tools.showToast(res.msg)
				}
			})
		},

		//申请提现
		applyWithdrawal({
			dispatch
		}, args) {
			post(applyWithdrawal, args).then(res => {
				if (res.code == 0) {
					tools.showToast(res.msg)
					setTimeout(() => {
						tools.back();
					}, 1000)
				} else {
					tools.showToast(res.msg)
				}
			})
		},
		//提现记录
		withdrawalRecord({
			dispatch,
			commit
		}, args) {
			post(withdrawalRecord, args).then(res => {
				if (res.code == 0) {
					commit('SET_WITHDRAW', res.data)
				} else {
					tools.showToast(res.msg)
				}
			})
		},
		//vip套餐列表
		vipListApi({
			dispatch,
			commit
		}, args = {}) {
			post(vipList, args).then(res => {
				if (res.code == 0) {
					commit('SET_VIP_LIST', res.data)
				} else {
					tools.showToast(res.msg)
				}
			})
		},

		//购买会员
		addVipOrder({
			dispatch
		}, args = {}) {
			post(addVipOrder, args).then(res => {
				if (res.code == 0) {
					tools.showToast(res.msg)
					setTimeout(() => {
						tools.back();
					}, 1000)
				} else {
					tools.showToast(res.msg)
				}
			})
		},
	},
}
